package com.wwwc.index.web.servlet;

import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequestWrapper;
import java.net.*;

import com.wwwc.util.web.*;

public class TopBarServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)
    {
	try {
	    response.setContentType("text/html");
	    PrintWriter out     = response.getWriter();
	    HttpSession session = request.getSession(true);
	    String user_ip      = request.getRemoteAddr();

	    out.println(MyXml.getComments("Top"));

	    Hashtable dir_hs = new Hashtable();
	    dir_hs = (Hashtable)session.getAttribute("ContralOpenClose");
	    
	    if(dir_hs == null) {
		System.out.println("TopBarServlet:Error:101");
		return;
	    }

	    String user_name   = null;
	    String user_age    = null;
	    String user_level  = null;
	    String user_fname  = null;
	    String user_since  = null;
	    String user_edu    = null;
	    String user_zip    = null;
	    String user_sex    = null;
	    String screen_w    = null;
	    String screen_h    = null;

	    Hashtable user_info = (Hashtable)session.getAttribute("user_info");
	    if(user_info == null) {
		System.out.println("TopBarServlet:Error:102");
		return;
	    }
	    else {
		user_name   = (String)user_info.get("user_name");
		user_age    = (String)user_info.get("user_age");
		user_level  = (String)user_info.get("user_level");
		user_fname  = (String)user_info.get("user_fname");
		user_since  = (String)user_info.get("user_since");
		user_edu    = (String)user_info.get("user_edu");
		user_zip    = (String)user_info.get("user_zip");
		user_sex    = (String)user_info.get("user_sex");
		screen_w    = (String)user_info.get("screen_w");
		screen_h    = (String)user_info.get("screen_h");
	    }

	    if(screen_w == null || screen_h == null) {
		screen_w = "1280";
		screen_h = "1024";
	    }

	    int sw = Integer.parseInt(screen_w);
	    int sh = Integer.parseInt(screen_h);

	    TopBarBean tbean = new TopBarBean();
	    String id   = (String)request.getParameter("id");
	    String tid  = (String)request.getParameter("tid");

	    String mid = (String)request.getAttribute("mid");
	    if(mid == null) {
		if(tid != null && tid.equals("adv")) {
		    String adv_id  = request.getParameter("adv_id");
		    tbean.updateAdCounter(adv_id);
		    out.println(tbean.replaceTopUrl(adv_id));
		}
		return;
	    }

	    String call_path = request.getContextPath()+request.getServletPath();
	    //String call_path = request.getServletPath();
	    String sid = (String)dir_hs.get(mid+"_sid");

	    int count = 0;	   	

	    //===============================================
	    // advs
	    //===============================================

	    if(tid == null || tid.equals("0")) {
		out.println(tbean.getTopAd(call_path, id, sid, 1, user_name));
	    }
	
	    //===============================================
	    // sign in
	    //===============================================
	    if(tid != null && tid.equals("1")) {
		out.println(MyJavaScript.disableHistory());
		if(session.getAttribute("count") == null) {
		    session.setAttribute("count", ""+count);
		}
		else {
		    count = Integer.parseInt((String)session.getAttribute("count"));
		}

		if(count < 10) {
		    out.println("<BR>"+tbean.getLoginForm(false, session.getId(), call_path, user_ip)+"<BR>");
		}
		else {
		    out.println("<CENTER><h3>You have been locked out</h3></CENTER>");
		}
	    }

	    if(tid != null && tid.equals("sign_in_check")) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String submit_id = request.getParameter("submit_id");

		if(!submit_id.equals(session.getId())) {
		    //System.out.println("TopBarServlet:Error:sign_in_check:submit_id <> sessionId");
		    //return;
		    System.out.println("TopBarServlet:Error:sign_in_check:submit_id="+submit_id+": sessionId="+session.getId());
		}

		if(!tbean.checkUsernamePassword(username, password)) {
		    count = Integer.parseInt((String)session.getAttribute("count"));
		    session.setAttribute("count",""+(count+1));
		    if(count > 10) {
			out.println("<CENTER><h3>You have been locked out</h3></CENTER>");
			return;
		    }
		    if(count > 1) {
			out.println("<BR>"+tbean.getLoginForm(true, session.getId(), call_path, user_ip)+"<BR>");
		    }
		    else {
			out.println("<BR>"+tbean.getLoginForm(false, session.getId(), call_path, user_ip)+"<BR>");
		    }
		}
		else {
		    if(!tbean.setUserProfile(request, username, user_ip)) {
			out.println(tbean.getUserMessage());
		    }
		    else {
			StringBuffer last_call = (StringBuffer)(session.getAttribute("last_call"));
			if(last_call != null) {
			    last_call.append("<input type='hidden' name='ReloadBar' value='true'>"); 
			    out.println(tbean.resetUrl(call_path, last_call));
			    return;
			}
			out.println(tbean.resetUrl(call_path+"?ReloadBar=true"));
		    }
		}
	    }
	    
	    //================================================
	    // e-mail password
	    //================================================

	    if(tid != null && tid.equals("forgot_pass")) {
		out.println(MyJavaScript.disableHistory());
		out.println(tbean.emailUserInfo(request));
	    }
	    
	    //================================================
	    // sign off
	    //================================================

	    if(tid != null && tid.equals("sign_off")) {
		session.invalidate();
		out.println(tbean.resetUrl(call_path));
	    }

	    out.println(MyXml.getComments("End of Top"));
	}
	catch(IOException e) {
	    System.out.println("TopBarServlet:Error:401:"+e);
	    return;
	}
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
	doPost(request, response);
    }
}






